<!DOCTYPE html>
<html>
  <head>
    <script src="../resources/js-test.js"></script>
  </head>
  <body>

    <textarea id="textarea" rows="3" cols="40">
Line 1
Line 2
Line 3
    </textarea>

    <textarea id="textarea-empty" rows="5" cols="40"></textarea>

    <script>
        description("This tests that caret position is reported correctly for textarea elements.");

        if (window.accessibilityController) {

            let textarea = document.getElementById('textarea');
            textarea.focus();
            window.textareaAccessible =
                accessibilityController.accessibleElementById('textarea');
            window.emptyTextareaAccessible =
                accessibilityController.accessibleElementById('textarea-empty');

            for (let i = 0; i < 3; ++i) {
                for (let j = 0; j < 7; ++j) {
                    let caretPosition = i * 7 + j;
                    textarea.selectionStart = caretPosition;
                    textarea.selectionEnd = caretPosition;
                    shouldBe("textareaAccessible", "textareaAccessible.selectionAnchorObject");
                    shouldBeEqualToNumber("textareaAccessible.selectionAnchorOffset",
                        caretPosition);
                    shouldBe("textareaAccessible", "textareaAccessible.selectionFocusObject");
                    shouldBeEqualToNumber("textareaAccessible.selectionFocusOffset",
                        caretPosition);
                }
            }

            let emptyTextarea = document.getElementById('textarea-empty');
            emptyTextarea.focus();
            // Each textarea remembers its own independent selection but
            // textareas that are not focused don't expose their selection
            // visually.
            shouldBe("emptyTextareaAccessible", "textareaAccessible.selectionAnchorObject");
            shouldBeEqualToNumber("textareaAccessible.selectionAnchorOffset", 0);
            shouldBe("emptyTextareaAccessible", "textareaAccessible.selectionFocusObject");
            shouldBeEqualToNumber("textareaAccessible.selectionFocusOffset", 0);

            textarea.focus();
            shouldBe("textareaAccessible", "textareaAccessible.selectionAnchorObject");
            shouldBeEqualToNumber("textareaAccessible.selectionAnchorOffset", 20);
            shouldBe("textareaAccessible", "textareaAccessible.selectionFocusObject");
            shouldBeEqualToNumber("textareaAccessible.selectionFocusOffset", 20);

            emptyTextarea.focus();
            shouldBe("emptyTextareaAccessible", "emptyTextareaAccessible.selectionAnchorObject");
            shouldBeZero("emptyTextareaAccessible.selectionAnchorOffset");
            shouldBe("emptyTextareaAccessible", "emptyTextareaAccessible.selectionFocusObject");
            shouldBeZero("emptyTextareaAccessible.selectionFocusOffset");

            // Setting the caret at an invalid offset should not move it.
            emptyTextarea.setSelectionRange(1, 1);
            shouldBeZero("emptyTextareaAccessible.selectionAnchorOffset");
            shouldBeZero("emptyTextareaAccessible.selectionFocusOffset");

        }
    </script>

  </body>
</html>
